package com.crazy.writer.handlers.console.action;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.UIManager;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import com.crazy.writer.book.BookStorage;
import com.crazy.writer.handlers.console.view.WorkSpaceView;


public class SelectAction extends Action {
    
    private JFileChooser chooser;
    
    public SelectAction() {
        ImageDescriptor image = AbstractUIPlugin.imageDescriptorFromPlugin("com.crazy.writer", "icons/project.png");
        setImageDescriptor(image);
        try {
	    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	} catch (Exception e) {
	    e.printStackTrace();
	}
        chooser = new JFileChooser();
    }

    @Override
    public void run() {
	chooser.setCurrentDirectory(chooser.getSelectedFile());
	chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
	chooser.setMultiSelectionEnabled(false);
	int value=chooser.showOpenDialog(null);
	chooser.setVisible(true);
	
	if (value==chooser.APPROVE_OPTION) {
	    File file = chooser.getSelectedFile();
	    BookStorage.loadPath(file);
	    WorkSpaceView.resultText.setText(BookStorage.loadHistory(3));
	}
	
    }

}
